//
//  UserBaseController.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/25.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserBaseController.h"
#import "UserCenterCell.h"

#import "UserItem.h"
#import "UserGroup.h"
#import "UserArrowItem.h"

#define kSectionHeadViewHeight 20

@interface UserBaseController ()

@end

@implementation UserBaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTableviewStyle];
    [self configureNav];
    
}

#pragma mark 设置Nav的样式
- (void)configureNav{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sport_icon_blackbackArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 设置tableView的样式
- (void)setTableviewStyle {
    // 下面两个人一设置一个就可以
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 0;
    
    // 设置背景色
    self.tableView.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    // 去掉分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    UserGroup *group = self.groups[section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserCenterCell *cell = [UserCenterCell settingViewCellTableView:tableView];
    
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    cell.item = item;
    if (indexPath.row == group.items.count - 1) {
        [cell showLine:NO];
    }else {
        [cell showLine:YES];
    }
    
    if (indexPath.row == 0) {
        [cell showSectionSepTopLineView:YES];
    }else {
        [cell showSectionSepTopLineView:NO];
    }
    
    return cell;
}
// 选中的哪一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 取出对应的模型数据
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    
    // 2.判断有没有block,有的话就执行
    if (item.block) {
        item.block();
        return;
    }
    // 3.跳转
    if ([item isKindOfClass:[UserArrowItem class]]) {
        // 强转
        UserArrowItem *arrowItem = (UserArrowItem *)item;
        // 判断target是否有值
        if (arrowItem.target) {
            Class class = arrowItem.target;
            UIViewController *vc = [[class alloc] init];
            // 设置头部标题
            vc.title = item.title;
            
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
}



#pragma mark 返回组不或者时尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    
    return [self.groups[section] footerTitle];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UserGroup *group = self.groups[section];
  
    if (group.headerTitle != nil) {
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, group.headHeight)];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, kYFScreenWidth - 20, headView.bounds.size.height)];
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = [UIColor colorWithHex:0xaaaaaa];

        label.text = group.headerTitle;
        
        [headView addSubview:label];
        
        headView.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
        
        return headView;
    }else {
        return [[UIView alloc] init];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    UserGroup *group = self.groups[section];
    
    if (group.headHeight <= 0) {
        return kSectionHeadViewHeight;
    }else {
        return group.headHeight;
    }
}

#pragma mark 重写初始化方法
- (instancetype)init {
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        
    }
    return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        
    }
    return self;
}

#pragma mark 懒加载
- (NSMutableArray *)groups {
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}



@end
